package cn.kgc.crowdfundingadmin2.service;

import cn.kgc.crowdfundingadmin2.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadmin2.mapper.AdminMapper;
import cn.kgc.crowdfundingadmin2.pojo.Admin;
import cn.kgc.crowdfundingadmin2.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;
    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public Admin login(String loginAcct, String userPswd) {
        //根据loginacct查询admin
        //判断是否为空
        //如果不是空，把密码转换为密文
        //对比数据库中的密码
        //如果一致，登录成功，如果不一致，登录失败
        Admin admin=null;
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andLoginacctEqualTo(loginAcct);

        List<Admin> list = adminMapper.selectByExample(example);
        if (CrowdFundingUtils.collectionEffective(list)){
            admin=list.get(0);
            String md5 = CrowdFundingUtils.md5(userPswd);
            if (md5.equals(admin.getUserpswd())){
                //登录成功
                return admin;
            }else{
                //登录失败
                admin=null;
            }
        }
        return admin;
    }

    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        //设置当前页
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        //keyword没有值查全部，有值根据账号或者名称或者	邮箱地址模糊查询
        if (CrowdFundingUtils.stringEffective(keyword)){
             AdminExample example=new AdminExample();
            AdminExample.Criteria criteria1 = example.createCriteria();
            criteria1.andCreatetimeLike("%"+keyword+"%");
            AdminExample.Criteria criteria2 = example.createCriteria();
            criteria2.andUsernameLike("%"+keyword+"%");
            AdminExample.Criteria criteria3 = example.createCriteria();
            criteria3.andEmailLike("%"+keyword+"%");

            example.or(criteria2);
            example.or(criteria3);

            List<Admin> list = adminMapper.selectByExample(example);
            PageInfo<Admin> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }else{
            //查询全部
            List<Admin> list = adminMapper.selectByExample(null);
            PageInfo<Admin> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }
    }

    @Override
    public void remove(List<Integer> admins) {
        //delete from 表 where id in (1,2,3)
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(admins);
        adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        //密码加密
        String s=CrowdFundingUtils.md5(admin.getUserpswd());
        admin.setUserpswd(s);
        //存储
        adminMapper.insert(admin);
    }

    @Override
    public Admin getAdminById(Integer adminid) {
        return adminMapper.selectByPrimaryKey(adminid);
    }

    @Override
    public void updateAdmin(Admin admin) {
        //加密
        if (CrowdFundingUtils.stringEffective(admin.getUserpswd())){
            String s=CrowdFundingUtils.md5(admin.getUserpswd());
            admin.setUserpswd(s);
        }
        //更新数据库
        adminMapper.updateByPrimaryKeySelective(admin);
    }
}
